package repositories

import (
	"github.com/mlogclub/simple/sqls"
	"github.com/mlogclub/simple/web/params"
	"gorm.io/gorm"

	"bbs-go/internal/models"
)

var UserDepartmentRepository = newUserDepartmentRepository()

func newUserDepartmentRepository() *userDepartmentRepository {
	return &userDepartmentRepository{}
}

type userDepartmentRepository struct {
}

func (r *userDepartmentRepository) Get(db *gorm.DB, id int64) *models.UserDepartment {
	ret := &models.UserDepartment{}
	if err := db.First(ret, "id = ?", id).Error; err != nil {
		return nil
	}
	return ret
}

func (r *userDepartmentRepository) Take(db *gorm.DB, where ...interface{}) *models.UserDepartment {
	ret := &models.UserDepartment{}
	if err := db.Take(ret, where...).Error; err != nil {
		return nil
	}
	return ret
}

func (r *userDepartmentRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.UserDepartment) {
	cnd.Find(db, &list)
	return
}

func (r *userDepartmentRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.UserDepartment {
	ret := &models.UserDepartment{}
	if err := cnd.FindOne(db, &ret); err != nil {
		return nil
	}
	return ret
}

func (r *userDepartmentRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.UserDepartment, paging *sqls.Paging) {
	return r.FindPageByCnd(db, &params.Cnd)
}

func (r *userDepartmentRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.UserDepartment, paging *sqls.Paging) {
	cnd.Find(db, &list)
	count := cnd.Count(db, &models.UserDepartment{})

	paging = &sqls.Paging{
		Page:  cnd.Paging.Page,
		Limit: cnd.Paging.Limit,
		Total: count,
	}
	return
}

func (r *userDepartmentRepository) Create(db *gorm.DB, t *models.UserDepartment) (err error) {
	err = db.Create(t).Error
	return
}

func (r *userDepartmentRepository) Update(db *gorm.DB, t *models.UserDepartment) (err error) {
	err = db.Save(t).Error
	return
}

func (r *userDepartmentRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
	err = db.Model(&models.UserDepartment{}).Where("id = ?", id).Updates(columns).Error
	return
}

func (r *userDepartmentRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
	err = db.Model(&models.UserDepartment{}).Where("id = ?", id).UpdateColumn(name, value).Error
	return
}

func (r *userDepartmentRepository) Delete(db *gorm.DB, id int64) {
	db.Delete(&models.UserDepartment{}, "id = ?", id)
}

func (r *userDepartmentRepository) FindDepartmentList(db *gorm.DB, adminId int) (list []models.UserDepartment, err error) {
	db = db.Model(&models.UserDepartment{}).Order("sort_no asc")
	if adminId > 0 {
		db = db.Where("admin_id = ?", adminId)
	}
	err = db.Find(&list).Error
	return list, err
}

func (r *userDepartmentRepository) FindDepartmentSimpleList(db *gorm.DB) (list []models.UserDepartmentSimpleResponse, err error) {
	userDepartmentList := []models.UserDepartment{}
	err = db.Model(&models.UserDepartment{}).Order("sort_no asc").Find(&userDepartmentList).Error
	for _, userDepartment := range userDepartmentList {
		list = append(list, models.UserDepartmentSimpleResponse{
			Id:   userDepartment.Id,
			Name: userDepartment.Name,
		})
	}
	return list, err
}
